package com.qiyi.player.wonder.plugins.scenetile.view
{
   import com.iqiyi.components.panelSystem.impls.BasePanel;
   import com.qiyi.player.wonder.common.status.Status;
   import com.qiyi.player.wonder.common.vo.UserInfoVO;
   import scenetile.PlayBtn;
   import scenetile.FullhdTip;
   import scenetile.FullhdTipCloseBtn;
   import flash.text.TextField;
   import flash.display.Loader;
   import flash.display.Sprite;
   import flash.display.Shape;
   import com.iqiyi.components.global.GlobalStage;
   import gs.TweenLite;
   import flash.geom.Point;
   import com.qiyi.player.wonder.plugins.scenetile.SceneTileDef;
   import flash.display.DisplayObjectContainer;
   import com.qiyi.player.wonder.common.config.FlashVarConfig;
   import flash.events.MouseEvent;
   import com.qiyi.player.wonder.common.ui.FastCreator;
   import flash.events.Event;
   import flash.events.IOErrorEvent;
   import flash.events.SecurityErrorEvent;
   import flash.net.URLRequest;
   
   public class SceneTileToolView extends BasePanel
   {
      
      public function SceneTileToolView(param1:DisplayObjectContainer, param2:Status, param3:UserInfoVO) {
         super(NAME,param1);
         this._status = param2;
         this._userInfoVO = param3;
         this.initUI();
         this.onResize(GlobalStage.stage.stageWidth,GlobalStage.stage.stageHeight);
      }
      
      public static const NAME:String = "com.qiyi.player.wonder.plugins.scenetile.view.SceneTileToolView";
      
      private var _status:Status;
      
      private var _userInfoVO:UserInfoVO;
      
      private var _playBtn:PlayBtn;
      
      private var _1080pTip:FullhdTip;
      
      private var _tipCloseBtn:FullhdTipCloseBtn;
      
      private var _1080pTF:TextField;
      
      private var _fullHDBtnX:int;
      
      private var _gap:int;
      
      private var _loader:Loader;
      
      private var _imageContainer:Sprite;
      
      private var _border:Shape;
      
      public function get playBtn() : PlayBtn {
         return this._playBtn;
      }
      
      public function set fullHDBtnX(param1:int) : void {
         this._fullHDBtnX = param1 - 2;
         this._1080pTip.x = this._fullHDBtnX + 25 - this._1080pTip.width / 2;
      }
      
      public function set fullHDBtnY(param1:int) : void {
         this._gap = param1;
         this._1080pTip.y = GlobalStage.stage.stageHeight - this._1080pTip.height - this._gap;
      }
      
      public function setGap(param1:int) : void {
         this._gap = param1;
         var _loc2_:int = GlobalStage.stage.stageHeight - this._1080pTip.height - this._gap;
         TweenLite.to(this._1080pTip,0.5,
            {
               "y":_loc2_,
               "onComplete":this.onTweenComplete
            });
      }
      
      public function get fullHDTipX() : int {
         return localToGlobal(new Point(this._1080pTip.x,0)).x;
      }
      
      public function onUserInfoChanged(param1:UserInfoVO) : void {
         this._userInfoVO = param1;
      }
      
      public function updateFullHDTip() : void {
         this._1080pTip.x = this._fullHDBtnX + 25 - this._1080pTip.width / 2;
         this._1080pTip.y = GlobalStage.stage.stageHeight - this._1080pTip.height - this._gap;
      }
      
      public function onAddStatus(param1:int) : void {
         this._status.addStatus(param1);
         switch(param1)
         {
            case SceneTileDef.STATUS_TOOL_OPEN:
               this.open();
               break;
            case SceneTileDef.STATUS_PLAY_BTN_SHOW:
               addChild(this._playBtn);
               break;
            case SceneTileDef.STATUS_FULL_HD_TIP_SHOW:
               this._1080pTip.x = this._fullHDBtnX + 25 - this._1080pTip.width / 2;
               addChild(this._1080pTip);
               break;
         }
      }
      
      public function onRemoveStatus(param1:int) : void {
         this._status.removeStatus(param1);
         switch(param1)
         {
            case SceneTileDef.STATUS_TOOL_OPEN:
               this.close();
               break;
            case SceneTileDef.STATUS_PLAY_BTN_SHOW:
               if(this._playBtn.parent)
               {
                  this._playBtn.gotoAndStop(1);
                  removeChild(this._playBtn);
               }
               break;
            case SceneTileDef.STATUS_FULL_HD_TIP_SHOW:
               if(this._1080pTip.parent)
               {
                  removeChild(this._1080pTip);
               }
               break;
         }
      }
      
      public function onResize(param1:int, param2:int) : void {
         this._playBtn.x = 18;
         this._playBtn.y = param2 - 131;
         if(this._imageContainer != null)
         {
            this._imageContainer.width = GlobalStage.stage.stageWidth;
            this._imageContainer.height = GlobalStage.stage.stageHeight;
         }
         this.updateFullHDTip();
      }
      
      public function drawBorder() : void {
         this._border.graphics.clear();
         this._border.graphics.lineStyle(1,1579032);
         this._border.graphics.moveTo(0,0);
         this._border.graphics.lineTo(GlobalStage.stage.stageWidth - 1,0);
         this._border.graphics.lineTo(GlobalStage.stage.stageWidth - 1,GlobalStage.stage.stageHeight - 1);
         this._border.graphics.lineTo(0,GlobalStage.stage.stageHeight - 1);
         this._border.graphics.lineTo(0,0);
      }
      
      public function clearBorder() : void {
         this._border.graphics.clear();
      }
      
      override public function open(param1:DisplayObjectContainer = null) : void {
         if(!isOnStage)
         {
            super.open(param1);
            dispatchEvent(new SceneTileEvent(SceneTileEvent.Evt_ToolOpen));
         }
      }
      
      override public function close() : void {
         if(isOnStage)
         {
            super.close();
            dispatchEvent(new SceneTileEvent(SceneTileEvent.Evt_ToolClose));
         }
      }
      
      override protected function onAddToStage() : void {
         super.onAddToStage();
      }
      
      override protected function onRemoveFromStage() : void {
         super.onRemoveFromStage();
      }
      
      private function initUI() : void {
         this._imageContainer = new Sprite();
         if(!FlashVarConfig.autoPlay)
         {
            addChild(this._imageContainer);
            this._imageContainer.graphics.beginFill(0);
            this._imageContainer.graphics.drawRect(0,0,GlobalStage.stage.stageWidth,GlobalStage.stage.stageHeight);
            this._imageContainer.graphics.endFill();
            this._imageContainer.mouseEnabled = this._imageContainer.mouseChildren = false;
         }
         this._playBtn = new PlayBtn();
         this._playBtn.activeArea.alpha = 0;
         this._playBtn.buttonMode = true;
         this._playBtn.useHandCursor = true;
         this._playBtn.addEventListener(MouseEvent.MOUSE_OVER,function(param1:MouseEvent):void
         {
            _playBtn.gotoAndStop(2);
         });
         this._playBtn.addEventListener(MouseEvent.MOUSE_OUT,function(param1:MouseEvent):void
         {
            _playBtn.gotoAndStop(1);
         });
         if(this._status.hasStatus(SceneTileDef.STATUS_PLAY_BTN_SHOW))
         {
            addChild(this._playBtn);
         }
         this._1080pTip = new FullhdTip();
         this._1080pTF = FastCreator.createLabel("立即加入收藏",16777215);
         this._1080pTF.x = (this._1080pTip.width - this._1080pTF.width) / 2 - 5;
         this._1080pTF.y = 0;
         this._1080pTip.addChild(this._1080pTF);
         this._tipCloseBtn = new FullhdTipCloseBtn();
         this._tipCloseBtn.x = this._1080pTip.width - this._tipCloseBtn.width - 3;
         this._tipCloseBtn.y = (this._1080pTip.height - this._tipCloseBtn.height) / 2 - 3;
         this._1080pTip.addChild(this._tipCloseBtn);
         this._tipCloseBtn.addEventListener(MouseEvent.CLICK,this.onTipCloseBtnClick);
         this._border = new Shape();
         GlobalStage.stage.addChild(this._border);
      }
      
      private function onTweenComplete() : void {
         this._1080pTip.x = this._fullHDBtnX + 25 - this._1080pTip.width / 2;
         this._1080pTip.y = GlobalStage.stage.stageHeight - this._1080pTip.height - this._gap;
      }
      
      public function requestUnAutoPlayImage() : void {
         if((FlashVarConfig.autoPlay) && FlashVarConfig.imageUrl == "")
         {
            return;
         }
         this.destroyImageLoader();
         this._loader = new Loader();
         this._loader.contentLoaderInfo.addEventListener(Event.COMPLETE,this.onComplete);
         this._loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,this.onIOError);
         this._loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onIOError);
         this._loader.load(new URLRequest(FlashVarConfig.imageUrl));
      }
      
      private function onComplete(param1:Event) : void {
         if(isOnStage)
         {
            this._imageContainer.addChild(this._loader);
            this._loader.width = GlobalStage.stage.stageWidth;
            this._loader.height = GlobalStage.stage.stageHeight;
         }
      }
      
      private function onIOError(param1:Event) : void {
         trace("===================================>" + param1.toString());
      }
      
      public function destroyImageLoader() : void {
         if(this._loader == null)
         {
            return;
         }
         if(this._loader.parent)
         {
            this._imageContainer.removeChild(this._loader);
         }
         if(!(this._imageContainer == null) && (this._imageContainer.parent))
         {
            this._imageContainer.graphics.clear();
            removeChild(this._imageContainer);
         }
         this._loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,this.onComplete);
         this._loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,this.onIOError);
         this._loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onIOError);
         this._loader = null;
      }
      
      private function onTipCloseBtnClick(param1:MouseEvent) : void {
         dispatchEvent(new SceneTileEvent(SceneTileEvent.Evt_TipCloseBtnClick));
      }
   }
}
